package storage;

import javax.microedition.rms.RecordComparator;

/**
 *  Implementation of RecordComparator for Persistent classes
 *  
 * @author Marcelo Gatto
 */
public abstract class PersistentComparator implements RecordComparator {

	private Class persistentType;
	
	/**
	 * Injected by PersistentDAO
	 */
	public void setPersistentType(Class perType) {
		persistentType = perType;
	}
	
	public int compare(byte[] b1, byte[] b2) {
		if(persistentType==null) return EQUIVALENT;
		try {
			Persistent p1 = (Persistent) persistentType.newInstance();
			p1.resurrect(b1);
			Persistent p2 = (Persistent) persistentType.newInstance();
			p2.resurrect(b2);
			return compare(p1, p2);
		} catch (Exception e) {
			e.printStackTrace();
			return EQUIVALENT;
		}
	}
	
	public abstract int compare(Persistent p1, Persistent p2);

}